package com.xu.sys.dao.impl;

import com.xu.sys.bean.SysMenu;
import com.xu.sys.dao.IMenuDao;
import com.xu.sys.utils.MyDbutils;
import com.xu.sys.utils.PageUtils;
import com.xu.sys.utils.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuDaoimpl implements IMenuDao {
    @Override
    public List<SysMenu> list(SysMenu entity) {
        //查询所有信息
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select * from sys_menu order by  seq asc";
        try {
            List<SysMenu> list = queryRunner.query(sql, new ResultSetHandler<List<SysMenu>>() {
                List<SysMenu> list = new ArrayList<>();
                @Override
                public List<SysMenu> handle(ResultSet resultSet) throws SQLException {
                    while (resultSet.next()) {
                        SysMenu entity = new SysMenu();
                        entity.setId(resultSet.getInt("id"));
                        entity.setName(resultSet.getString("name"));
                        entity.setUrl(resultSet.getString("url"));
                        entity.setParentId(resultSet.getInt("parent_id"));
                        entity.setSeq(resultSet.getInt("seq"));
                        entity.setCreateTime(resultSet.getDate("create_time"));
                        list.add(entity); // 把查询的记录封装到了集合容器中
                    }
                    return list;
                }
            });
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<SysMenu> listPage(PageUtils pageUtils) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select * from sys_menu  where parent_id = -1 limit ?,?";
        if (StringUtils.isNotEmpty(pageUtils.getKey())) {
            // 需要带条件查询
            sql = "select * from sys_menu  where parent_id = -1 and name like '%" + pageUtils.getKey() + "%' limit ?,?";
        }
        // 计算 分页开始的位置
        int startNo = pageUtils.getStart();
        try {
            List<SysMenu> list = queryRunner.query(sql, new ResultSetHandler<List<SysMenu>>() {
                List<SysMenu> list = new ArrayList<>();

                @Override
                public List<SysMenu> handle(ResultSet resultSet) throws SQLException {
                    while (resultSet.next()) {
                        SysMenu entity = new SysMenu();
                        entity.setId(resultSet.getInt("id"));
                        entity.setName(resultSet.getString("name"));
                        entity.setUrl(resultSet.getString("url"));
                        entity.setParentId(resultSet.getInt("parent_id"));
                        entity.setSeq(resultSet.getInt("seq"));
                        entity.setCreateTime(resultSet.getDate("create_time"));
                        list.add(entity); // 把查询的记录封装到了集合容器中
                    }
                    return list;
                }
            }, startNo, pageUtils.getPageSize());
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int save(SysMenu entity) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "insert into sys_menu(name,url,parent_id,seq)values(?,?,?,?)";
        try {
            return queryRunner.update(sql, entity.getName(), entity.getUrl(), entity.getParentId(), entity.getSeq());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return 0;
    }

    @Override
    public SysMenu findById(int id) {
        //查询所有角色
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select * from sys_menu where id = ?";
        try {
            return queryRunner.query(sql, new ResultSetHandler<SysMenu>() {
                List<SysMenu> list = new ArrayList<>();
                @Override
                public SysMenu handle(ResultSet resultSet) throws SQLException {
                    if (resultSet.next()) {
                        SysMenu entity = new SysMenu();
                        entity.setId(resultSet.getInt("id"));
                        entity.setName(resultSet.getString("name"));
                        entity.setUrl(resultSet.getString("url"));
                        entity.setParentId(resultSet.getInt("parent_id"));
                        entity.setSeq(resultSet.getInt("seq"));
                        entity.setCreateTime(resultSet.getDate("create_time"));
                        return entity;
                    }
                    return null;
                }
            }, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int updteById(SysMenu entity) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "UPDATE sys_menu SET name = ?, url = ?, parent_id = ?, seq = ? WHERE id = ?";
        try {
            return queryRunner.update(sql, entity.getName(), entity.getUrl(), entity.getParentId(), entity.getSeq(), entity.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteById(int id) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "delete from  sys_menu where id = ?";
        try {
            return queryRunner.update(sql, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int count(PageUtils pageUtils) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select count(1) from sys_menu ";
        if (StringUtils.isNotEmpty(pageUtils.getKey())) {
            sql = "select count(1) from sys_menu  where name like '%" + pageUtils.getKey() + "%' or nickname like '%" + pageUtils.getKey() + "%' ";
        }
        try {
            return queryRunner.query(sql, new ResultSetHandler<Integer>() {
                @Override
                public Integer handle(ResultSet resultSet) throws SQLException {
                    resultSet.next();
                    return resultSet.getInt(1);
                }
            });
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<SysMenu> getAllParent() {
        //查询所有角色
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select * from sys_menu where parent_id = -1";
        try {
            List<SysMenu> list = queryRunner.query(sql, new ResultSetHandler<List<SysMenu>>() {
                List<SysMenu> list = new ArrayList<>();
                @Override
                public List<SysMenu> handle(ResultSet resultSet) throws SQLException {
                    while (resultSet.next()) {
                        SysMenu entity = new SysMenu();
                        entity.setId(resultSet.getInt("id"));
                        entity.setName(resultSet.getString("name"));
                        entity.setUrl(resultSet.getString("url"));
                        entity.setParentId(resultSet.getInt("parent_id"));
                        entity.setSeq(resultSet.getInt("seq"));
                        entity.setCreateTime(resultSet.getDate("create_time"));
                        list.add(entity); // 把查询的记录封装到了集合容器中
                    }
                    return list;
                }
            });
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean isDispatcher(int id) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select count(1) from sys_role_menu where menu_id =  ?";
        try {
            queryRunner.query(sql, new ResultSetHandler<Boolean>() {
                @Override
                public Boolean handle(ResultSet resultSet) throws SQLException {
                    resultSet.next();
                    int count = resultSet.getInt(1);
                    return count > 0 ? true : false;
                }
            },id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public boolean havaSubMenu(int id) {
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select count(1) from sys_menu where parent_id = ?";
        try {
            queryRunner.query(sql, new ResultSetHandler<Boolean>() {
                @Override
                public Boolean handle(ResultSet resultSet) throws SQLException {
                    resultSet.next();
                    int count = resultSet.getInt(1);
                    return count > 0 ? true : false;
                }
            },id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public List<SysMenu> findByRoleId(Integer roleId) {
        //查询所有信息
        QueryRunner queryRunner = MyDbutils.getQueryRunner();
        String sql = "select * from sys_menu where id in (select menu_id from sys_role_menu where role_id = ?) order by  seq asc";
        try {
            List<SysMenu> list = queryRunner.query(sql, new ResultSetHandler<List<SysMenu>>() {
                List<SysMenu> list = new ArrayList<>();
                @Override
                public List<SysMenu> handle(ResultSet resultSet) throws SQLException {
                    while (resultSet.next()) {
                        SysMenu entity = new SysMenu();
                        entity.setId(resultSet.getInt("id"));
                        entity.setName(resultSet.getString("name"));
                        entity.setUrl(resultSet.getString("url"));
                        entity.setParentId(resultSet.getInt("parent_id"));
                        entity.setSeq(resultSet.getInt("seq"));
                        entity.setCreateTime(resultSet.getDate("create_time"));
                        list.add(entity); // 把查询的记录封装到了集合容器中
                    }
                    return list;
                }
            },roleId);
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}

